package com.cykj.eurekaclient.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cykj.eurekaclient.bean.*;

import com.cykj.eurekaclient.service.inter.AdminService;
import com.cykj.eurekaclient.utils.JsonXMLUtils;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.*;

@RestController
public class HelloController {

    @Autowired
    private AdminService adminService;

    @RequestMapping("/hello")
    public String name(String name) {
        System.out.println("进入生产者");
        return "成功进入生产者";
    }

    @RequestMapping("/getInfo")
    public int getDemoInfo() {
        int a = adminService.selUserAll();
        System.out.println("getInfo");
        System.out.println("从数据库获取的总条数:" + a);
        return a;
    }

    @RequestMapping("/getString")
    public String getDemoInfo1(String userId) {
        return userId + ",getString";
    }

    @RequestMapping("/getUser")
    public User getUser(@RequestBody User user) {
        System.out.println("111");
        return user;
    }

    //----------------  标记站点，配置信息---------------
    @RequestMapping("getTest")
    public String getTest(@RequestBody StationInfo stationInfo) {
        System.out.println("进来了");
        System.out.println(stationInfo.getName());
        System.out.println("经度" + stationInfo.getLongitude());
        System.out.println("纬度" + stationInfo.getLatitude());
        System.out.println("城市" + stationInfo.getCityStr());
        System.out.println("区/县" + stationInfo.getDistrict());
        List<StationInfo> findsite = adminService.findSiteNameInfo(stationInfo.getName());
        List<CityInfo> cityExist = adminService.selCityExist(stationInfo.getCityStr(),stationInfo.getDistrict());
        if (findsite.size()==0 && cityExist.size()!=0){
            StationInfo sta = new StationInfo();
            sta.setName(stationInfo.getName());
            sta.setLongitude(stationInfo.getLongitude());
            sta.setLatitude(stationInfo.getLatitude());
            adminService.insertSite(sta,stationInfo.getCityStr());
            return "1";
        }else {
            return "2";
        }
    }

    //---------------- 站点配置查询 ---------------
    @RequestMapping("getSite")
    public Map<String,Object> getSite(@RequestBody Map<String,Object> map) throws Exception {
        System.out.println("站点配置查询");
        PageConfig pageConfig = null;
        StationInfo stationInfo = null;
        if (map.get("pageConfig")!=null){
            pageConfig = JsonXMLUtils.map2obj((Map<String, Object>)map.get("pageConfig"),PageConfig.class);
        }
        if (map.get("stationInfo")!=null){
            stationInfo = JsonXMLUtils.map2obj((Map<String, Object>) map.get("stationInfo"),StationInfo.class);
            System.out.println(stationInfo.getName() + ":**********");
        }
        Map<String,Object> site = new HashMap<>();
        String name = "%" + stationInfo.getName() + "%";
        site.put("pageConfig",pageConfig);
        site.put("count",adminService.siteCount(name));
        site.put("siteConfig",adminService.findSiteInfo(new RowBounds((pageConfig.getPage() -1) * pageConfig.getSize(),pageConfig.getSize()),name));
        return site;
    }

    //---------------- 站点配置删除数据 ---------------
    @RequestMapping("deleteRow")
    public boolean deleteRow(@RequestBody StationInfo stationInfo){
        System.out.println("删除行");
        int id = (int) stationInfo.getId();
        System.out.println("--站点配置--删除id为："+id);
        return adminService.deleteRow(id);
    }

    //---------------- 站点配置编辑数据 ---------------
    @RequestMapping("updateRow")
    public boolean updateRow(@RequestBody StationInfo stationInfo){
        System.out.println("编辑数据");
        System.out.println(stationInfo.getId());
        System.out.println(stationInfo.getName());
        System.out.println(stationInfo.getLongitude());
        System.out.println(stationInfo.getLatitude());
        System.out.println(stationInfo.getCityId());
        StationInfo sta = new StationInfo();
        sta.setName(stationInfo.getName());
        sta.setLongitude(stationInfo.getLongitude());
        sta.setLatitude(stationInfo.getLatitude());
        sta.setCityId(stationInfo.getCityId());
        boolean b = adminService.updateRow(sta, stationInfo.getId());
        if (b) {
            return true;
        }else {
            return false;
        }
    }

    //---------------- 线路配置查询数据 ---------------
    @RequestMapping("getLine")
    public Map<String,Object> getLineInfo(@RequestBody Map<String,Object> map) throws Exception {
        System.out.println("线路配置查询");
        PageConfig pageConfig = null;
        RouteInfo routeInfo = null;
        if (map.get("pageConfig")!=null){
            pageConfig = JsonXMLUtils.map2obj((Map<String, Object>)map.get("pageConfig"),PageConfig.class);
        }
        if (map.get("routeInfo")!=null){
            routeInfo = JsonXMLUtils.map2obj((Map<String, Object>) map.get("routeInfo"),RouteInfo.class);
        }
        System.out.println(routeInfo.getRouteName());
        Map<String,Object> line = new HashMap<>();
        String name = "%" + routeInfo.getRouteName() + "%";
        line.put("pageConfig",pageConfig);
        line.put("count",adminService.routeCount(name));
        line.put("lineConfig",adminService.getLineInfo(new RowBounds((pageConfig.getPage() -1) * pageConfig.getSize(),pageConfig.getSize()),name));
        return line;
    }

    //---------------- 线路配置删除数据 ---------------
    @RequestMapping("deleteRouteRow")
    public boolean deleteRouteRow(@RequestBody RouteInfo routeInfo){
        System.out.println("线路配置删除行");
        int id = (int) routeInfo.getId();
        System.out.println("--线路配置--删除id为："+id);
        return adminService.deleteRouteRow(id);
    }

    //---------------- 线路配置编辑数据 ---------------
    @RequestMapping("updateRouteRow")
    public boolean updateRouteRow(@RequestBody RouteInfo routeInfo){
        System.out.println("线路配置--编辑数据");
        System.out.println(routeInfo.getId());
        System.out.println(routeInfo.getRouteName());
        System.out.println(routeInfo.getPrice());
        System.out.println(routeInfo.getRouteTime());
        System.out.println(routeInfo.getDriverPrice());
        System.out.println(routeInfo.getIntervalTime());
        RouteInfo route = new RouteInfo();
        route.setRouteName(routeInfo.getRouteName());
        route.setPrice(routeInfo.getPrice());
        route.setRouteTime(routeInfo.getRouteTime());
        route.setDriverPrice(routeInfo.getDriverPrice());
        route.setIntervalTime(routeInfo.getIntervalTime());
        boolean b = adminService.updateRouteRow(route, routeInfo.getId());
        if (b) {
            return true;
        }else {
            return false;
        }
    }

    //----------------  添加线路--配置信息---------------
    @RequestMapping("insertRoute")
    public boolean insertRoute(@RequestBody RouteInfo routeInfo) {
        System.out.println("正在添加数据");
        System.out.println(routeInfo.getRouteName());
        System.out.println(routeInfo.getPrice());
        System.out.println(routeInfo.getRouteTime());
        System.out.println(routeInfo.getDriverPrice());
        System.out.println(routeInfo.getIntervalTime());
        List<RouteInfo> findRou = adminService.findRouteName(routeInfo.getRouteName());
        if (findRou.size()==0&&routeInfo.getPrice()!=null&&routeInfo.getRouteTime()!=null
                &&routeInfo.getDriverPrice()!=null&&routeInfo.getIntervalTime()!=null){
            RouteInfo rou = new RouteInfo();
            rou.setRouteName(routeInfo.getRouteName());
            rou.setPrice(routeInfo.getPrice());
            rou.setRouteTime(routeInfo.getRouteTime());
            rou.setDriverPrice(routeInfo.getDriverPrice());
            rou.setIntervalTime(routeInfo.getIntervalTime());
            adminService.insertRoute(rou);
            return true;
        }else {
            return false;
        }
    }

    //---------------- 地图上显示所有站点 ---------------  未实现
    @RequestMapping("positionAllSite")
    public Map<String,Object> positionAllSite(){
        System.out.println("地图上显示所有站点");
        Map<String,Object> pmap = new HashMap<>();
        pmap.put("count",adminService.selectCount());
        pmap.put("name",adminService.findSiteInfo());
        pmap.put("lng",adminService.findLng());
        pmap.put("lat",adminService.findLat());
//        String str = JSONObject.toJSONString(pmap);
//        System.out.println(str);
        return pmap;
    }

    //---------------- 班车班次时刻表配置查询 ---------------
    @RequestMapping("getBusTime")
    public Map<String,Object> getBusTime(@RequestBody Map<String,Object> map) throws Exception {
        System.out.println("班车班次时刻表配置查询");
        PageConfig pageConfig = null;
        RouteTimeNode routeTimeNode = null;
        if (map.get("pageConfig")!=null){
            pageConfig = JsonXMLUtils.map2obj((Map<String, Object>)map.get("pageConfig"),PageConfig.class);
        }
        if (map.get("routeTimeNode")!=null){
            routeTimeNode = JsonXMLUtils.map2obj((Map<String, Object>) map.get("routeTimeNode"),RouteTimeNode.class);
        }
        System.out.println(routeTimeNode.getRouteStr()+"///////////");
        String name = "%" + routeTimeNode.getRouteStr() + "%";
        Map<String,Object> timeNode = new HashMap<>();
        timeNode.put("pageConfig",pageConfig);
        timeNode.put("count",adminService.selectBusTimeCount(name));
        timeNode.put("timeNode",adminService.getBusTime(new RowBounds((pageConfig.getPage() -1) * pageConfig.getSize(),pageConfig.getSize()),name));
        return timeNode;
    }

    //---------------- 班车班次时刻表配置删除数据 ---------------
    @RequestMapping("deleteBusTimeRow")
    public boolean deleteBusTimeRow(@RequestBody RouteTimeNode routeTimeNode){
        System.out.println("班车班次时刻表配置删除行");
        int id = (int) routeTimeNode.getId();
        System.out.println("--班车班次时刻表--删除id为："+id);
        return adminService.deleteBusTimeRow(id);
    }

    //---------------- 班车班次时刻表配置 ** 下拉框取值 ---------------
    @RequestMapping("routeIdOptions")
    public Map<String,Object> routeIdOptions(){
        System.out.println("routeIdOptions下拉框选项");
        Map<String,Object> routeIdMap = new HashMap<>();
        routeIdMap.put("routeIdOptions",adminService.routeIdOptions());
        return routeIdMap;
    }
    //---------------- 班车班次时刻表配置 ** 下拉框取值 ---------------
    @RequestMapping("timePOptions")
    public Map<String,Object> timePOptions(){
        System.out.println("timePOptions下拉框选项");
        Map<String,Object> timePOptionsMap = new HashMap<>();
        timePOptionsMap.put("timePOptions",adminService.timePOptions());
        return timePOptionsMap;
    }

    //---------------- 班车班次时刻表配置 ** 下拉框取值 ---------------
    @RequestMapping("stateOptions")
    public Map<String,Object> stateOptions(){
        System.out.println("stateOptions下拉框选项");
        Map<String,Object> stateOptionsMap = new HashMap<>();
        stateOptionsMap.put("stateOptions",adminService.stateOptions());
        return stateOptionsMap;
    }

    //----------------  添加时刻表--配置信息---------------
    @RequestMapping("insertBusTime")
    public boolean insertBusTime(@RequestBody RouteTimeNode routeTimeNode) {
        System.out.println("时刻表正在添加数据");
        System.out.println(routeTimeNode.getRouteStr());
        System.out.println(routeTimeNode.getTimeParagraphStr());
        System.out.println(routeTimeNode.getTimeNode());
        System.out.println(routeTimeNode.getState());
        RouteTimeNode rou = new RouteTimeNode();
        rou.setRouteStr(routeTimeNode.getRouteStr());
        rou.setTimeParagraphStr(routeTimeNode.getTimeParagraphStr());
        rou.setTimeNode(routeTimeNode.getTimeNode());
        rou.setState(routeTimeNode.getState());
        boolean b = adminService.insertBusTime(rou);
        if (b) {
            return true;
        }else {
            return false;
        }
    }

    //---------------- 时刻表配置编辑数据 ---------------
    @RequestMapping("updateBusTimeRow")
    public boolean updateBusTimeRow(@RequestBody RouteTimeNode routeTimeNode){
        System.out.println("时刻表配置--编辑数据");
        System.out.println(routeTimeNode.getId());
        System.out.println(routeTimeNode.getRouteStr());
        System.out.println(routeTimeNode.getTimeParagraphStr());
        System.out.println(routeTimeNode.getTimeNode());
        System.out.println(routeTimeNode.getState());
        boolean b = adminService.updateBusTimeRow(routeTimeNode.getRouteStr(),routeTimeNode.getTimeParagraphStr(),
                routeTimeNode.getTimeNode(),routeTimeNode.getState(),routeTimeNode.getId());
        if (b) {
            return true;
        }else {
            return false;
        }
    }

    //---------------- 城市信息配置查询 ---------------
    @RequestMapping("getCityInfo")
    public Map<String,Object> getCityInfo(@RequestBody Map<String,Object> map) throws Exception {
        System.out.println("城市信息配置查询");
        PageConfig pageConfig = null;
        CityInfo cityInfo = null;
        if (map.get("pageConfig")!=null){
            pageConfig = JsonXMLUtils.map2obj((Map<String, Object>)map.get("pageConfig"),PageConfig.class);
        }
        if (map.get("cityInfo")!=null){
            cityInfo = JsonXMLUtils.map2obj((Map<String, Object>) map.get("cityInfo"),CityInfo.class);
        }
        System.out.println(cityInfo.getName());
        Map<String,Object> city = new HashMap<>();
        String name = "%" + cityInfo.getName() + "%";
        city.put("pageConfig",pageConfig);
        city.put("count",adminService.cityCount(name));
        city.put("cityConfig",adminService.getCityInfo(new RowBounds((pageConfig.getPage() -1) * pageConfig.getSize(),pageConfig.getSize()),name));
        return city;
    }

    //---------------- 城市信息配置--添加数据 ---------------
    @RequestMapping("insertCity")
    public String insertCity(@RequestBody CityInfo cityInfo){
        System.out.println("城市信息配置--增加");
        System.out.println(cityInfo.getName() + "," + cityInfo.getParentName() + "," + cityInfo.getDistrict());
        List<CityInfo> findCityName = adminService.selCityExist(cityInfo.getName(),cityInfo.getDistrict());
        if (findCityName.size()==0){
            adminService.insertCity(cityInfo.getName(), cityInfo.getParentName(),cityInfo.getDistrict());
            return "1";
        }else {
            return "2";
        }
    }

    //---------------- 城市信息配置--修改数据 ---------------
    @RequestMapping("updateCityRow")
    public String updateCityRow(@RequestBody CityInfo cityInfo){
        System.out.println("城市信息配置--编辑");
        System.out.println(cityInfo.getName() + "," + cityInfo.getParentName() + "," + cityInfo.getId());
        List<CityInfo> findCityName = adminService.selCityExist(cityInfo.getName(),cityInfo.getDistrict());
        if (findCityName.size()==0) {
            adminService.updateCityRow(cityInfo.getName(),cityInfo.getParentName(),cityInfo.getDistrict(),cityInfo.getId());
            return "1";
        }else {
            return "2";
        }
    }

    //---------------- 城市信息配置删除数据 ---------------
    @RequestMapping("deleteCityInfoRow")
    public boolean deleteCityInfoRow(@RequestBody CityInfo cityInfo){
        System.out.println("城市信息配置删除行");
        int id = (int) cityInfo.getId();
        System.out.println("--城市信息配置--删除id为："+id);
        return adminService.deleteCityInfoRow(id);
    }



    //---------------- 行经站点配置查询 ---------------
    @RequestMapping("getTravelSite")
    public Map<String,Object> getTravelSite(@RequestBody Map<String,Object> map) throws Exception {
        System.out.println("行经站点配置查询");
        PageConfig pageConfig = null;
        StationAssign stationAssign = null;
        if (map.get("pageConfig")!=null){
            pageConfig = JsonXMLUtils.map2obj((Map<String, Object>)map.get("pageConfig"),PageConfig.class);
        }
        if (map.get("stationAssign")!=null){
            stationAssign = JsonXMLUtils.map2obj((Map<String, Object>) map.get("stationAssign"),StationAssign.class);
        }
        System.out.println(stationAssign.getRouteStr());
        Map<String,Object> travelSite = new HashMap<>();
        String name = "%" + stationAssign.getRouteStr() + "%";
        travelSite.put("pageConfig",pageConfig);
        travelSite.put("count",adminService.selectTravelSiteCount(name));
        travelSite.put("travelSiteConfig",adminService.getTravelSite(new RowBounds((pageConfig.getPage() -1) * pageConfig.getSize(),pageConfig.getSize()),name));
        return travelSite;
    }

    //---------------- 行经站点配置 ** 下拉框取值 ---------------
    @RequestMapping("routeNameOptions")
    public Map<String,Object> routeNameOptions(){
        System.out.println("routeNameOptions下拉框选项");
        Map<String,Object> routeNameMap = new HashMap<>();
        routeNameMap.put("routeNameOptions",adminService.routeNameOptions());
        return routeNameMap;
    }
    //---------------- 行经站点配置 ** 下拉框取值 ---------------
    @RequestMapping("stationOptions")
    public Map<String,Object> stationOptions(){
        System.out.println("stationOptions下拉框选项");
        Map<String,Object> stationMap = new HashMap<>();
        stationMap.put("stationOptions",adminService.stationOptions());
        return stationMap;
    }

    //---------------- 行经站点配置删除数据 ---------------
    @RequestMapping("deleteTravelSiteRow")
    public boolean deleteTravelSiteRow(@RequestBody StationAssign stationAssign){
        System.out.println("行经站点配置删除行");
        int id = (int) stationAssign.getId();
        System.out.println("--行经站点--删除id为："+id);
        return adminService.deleteTravelSiteRow(id);
    }

    //----------------  添加行经站点--配置信息---------------
    @RequestMapping("insertTravelSite")
    public boolean insertTravelSite(@RequestBody StationAssign stationAssign) {
        System.out.println("行经站点表正在添加数据");
        System.out.println(stationAssign.getRouteStr());
        System.out.println(stationAssign.getStationStr());
        System.out.println(stationAssign.getSequence());
        List<StationAssign> findTravelSiteName = adminService.findTravelSiteName(stationAssign.getStationStr(),stationAssign.getSequence());
        if (findTravelSiteName.size()==0){
            adminService.insertTravelSite(stationAssign.getRouteStr(),stationAssign.getStationStr(),stationAssign.getSequence());
            return true;
        }else {
            return false;
        }
    }

    //---------------- 行经站点配置编辑数据 ---------------
    @RequestMapping("updateTravelSiteRow")
    public boolean updateTravelSiteRow(@RequestBody StationAssign stationAssign){
        System.out.println("行经站点表配置--编辑数据");
        System.out.println(stationAssign.getId());
        System.out.println(stationAssign.getRouteStr());
        System.out.println(stationAssign.getStationStr());
        System.out.println(stationAssign.getSequence());
        boolean b = adminService.updateTravelSiteRow(stationAssign.getRouteStr(),stationAssign.getStationStr(),
                stationAssign.getSequence(),stationAssign.getId());
        if (b) {
            return true;
        }else {
            return false;
        }
    }

    @RequestMapping("findCollectMoney")
    public FrontUserFlowRecord findAllAgeP() {
        //new一个结果集
        FrontUserFlowRecord frontUserFlowRecord = new FrontUserFlowRecord();
        //调用查询年龄段的方法
        List<FrontUserFlowRecord> list = adminService.findCollectMoney();
        //定义List集合转化为纯数组
        List<Object> ints = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            //获取年龄
            ints.add(list.get(i).getTradeMoney());
        }
        List<Object> intStr = new ArrayList<>();
        List<FrontUserFlowRecord> listStr = adminService.collectMoneyRoute();
        for (int i = 0; i < listStr.size(); i++) {
            //获取年龄
            intStr.add(listStr.get(i).getRouteStr());
        }
        frontUserFlowRecord.setAllTakeIn(adminService.allTakeIn());
        frontUserFlowRecord.setData(ints);
        frontUserFlowRecord.setRouteData(intStr);
        return frontUserFlowRecord;
    }





}
